package cn.edu.neusoft.amicooldemo.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.edu.neusoft.amicooldemo.R;
import cn.edu.neusoft.amicooldemo.ViewarticleActivity;
import cn.edu.neusoft.amicooldemo.adapter.ArticleAdapter;
import cn.edu.neusoft.amicooldemo.bean.ArticleBean;
import cn.edu.neusoft.amicooldemo.factory.AmicoolFactory;
import cn.edu.neusoft.amicooldemo.factory.FocusUserFactory;
import cn.edu.neusoft.amicooldemo.iface.AmicoolListener;
import cn.edu.neusoft.amicooldemo.iface.OnRecyclerViewItemClickListener;


/**
 * A simple {@link Fragment} subclass.
 */
public class FArticleFragment extends Fragment {


    private RecyclerView recyclerView;

    private List<ArticleBean> list;
    protected ArticleAdapter adapter;
    private static int cur_page=1;private int lastVisibleItemPosition;
    private static String mod;
    private String SessionID;
    private SharedPreferences sp;
    private int userid;
    private AmicoolListener<ArticleBean> listener;

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public FArticleFragment() {
        // Required empty public constructor

        mod = "article";
        list = new ArrayList<ArticleBean>();

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_article, container, false);
        sp = getActivity().getSharedPreferences("amicool_user", Context.MODE_PRIVATE);
        SessionID=sp.getString("SessionID","dfrcg3211kjb1vbe3ibgftkfa6");
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView = (RecyclerView) view.findViewById(R.id.tab_article_list);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new ArticleAdapter(getActivity(),list);
        adapter.setOnItemClickListener(new ArticleAdapter.OnItemClickListener(){

            @Override
            public void onItemClick(View view, int position) {

                Intent intent=new Intent(getActivity(), ViewarticleActivity.class);
                intent.putExtra("resid",list.get(position).getId());
                intent.putExtra("userid",list.get(position).getUserid());
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

        recyclerView.setAdapter(adapter);
        listener = new AmicoolListener<ArticleBean>() {
            @Override
            public void onResponse(List<ArticleBean> l) {
                list.removeAll(l);
                list.addAll(0,l);
                adapter.setList(list);

            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(getActivity(),"fail:"+msg,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void loadPreData(List<ArticleBean> l) {

            }

            @Override
            public void loadNextData(List<ArticleBean> l) {
                list.addAll(l);
                adapter.notifyDataSetChanged();

            }

        };
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == adapter.getItemCount()) {
                    cur_page+=1;
                    FocusUserFactory.getInstance().getResultList(mod,cur_page,SessionID,userid,listener);

                }


            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();;

            }
        });
        if(cur_page==1)
        FocusUserFactory.getInstance().getResultList(mod,cur_page,SessionID,userid,listener);
        return view;
    }



}
